/*
 * @Author: liyin 2179253556@qq.com
 * @Date: 2023-03-20 13:51:35
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2025-05-21 16:06:46
 * @FilePath: vite.config.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineConfig } from 'vite'
import path from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import vue from '@vitejs/plugin-vue'
import px2rem from "postcss-px2rem"
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue(), AutoImport({
    resolvers: [ElementPlusResolver()],
  }),
  Components({
    resolvers: [ElementPlusResolver({
      importStyle: "sass"
    })],
    directoryAsNamespace: true
  }),
  ],
  resolve: {
    alias: {
      // 如果报错__dirname找不到，需要安装node,npm add @types/node --save-dev
      "@": path.resolve(__dirname, "src"),
      "coms": path.resolve(__dirname, "src/components"),
    }
  },
  css: {
    postcss: {
      plugins: [
        px2rem({
          remUnit: 16
        })
      ]
    },
    preprocessorOptions: {
      scss: {
        additionalData: `@use "@/assets/element/index.scss" as *;`,
      },
    },
  },
  // base: '/DataVisual/DigitalScreen/',
  base: '/',
  server: {
    // 服务器主机名，如果允许外部访问，可设置为"0.0.0.0"
    host: '0.0.0.0',
    // 服务器端口号
    port: 6011,
    // boolean | string 启动项目时自动在浏览器打开应用程序；如果为string，比如"/index.html"，会打开http://localhost:3000/index.html
    open: false,
    // 自定义代理规则
    proxy: {
      '/DataVisual': {
        // http://192.168.8.5
        target: 'http://192.168.8.5',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      }
      // '/Consume': {
      //   // http://192.168.8.5
      //   target: 'http://192.168.8.5',
      //   changeOrigin: true,
      //   rewrite: (path) => path.replace(/^\/api/, '')
      // }
    }
  },
  build: {
    // minify: 'terser',
    // 指定输出路径，默认'dist'
    outDir: 'dist',
    // 指定生成静态资源的存放路径(相对于build.outDir)
    assetsDir: 'assets',
    chunkSizeWarningLimit: 1600,
    // 小于此阈值的导入或引用资源将内联为base64编码，设置为0可禁用此项。默认4096（4kb）
    assetsInlineLimit: 4096,
    // 启用/禁用CSS代码拆分，如果禁用，整个项目的所有CSS将被提取到一个CSS文件中,默认true
    cssCodeSplit: true,
    // 构建后是否生成source map文件，默认false
    sourcemap: false,
    // 为true时，会生成manifest.json文件，用于后端集成
    manifest: false,
    rollupOptions: {
      output: {
        // chunkFileNames: "static/js/[name]-[hash].js",
        // entryFileNames: "static/js/[name]-[hash].js",
        // assetFileNames: "static/[ext]/[name]-[hash].[ext]",
        // 静态资源分拆打包
        manualChunks(id) {
          if (id.includes('node_modules')) {
            return id.toString().split('node_modules/')[1].split('/')[0].toString()
          }
        }
      },
    }
  }
})
